define(['jquery', 'api', 'user', 'util', 'Vue', 'text!pages/user/index.html'], function($, api, user, util, Vue, tpl) {
	var $loginForm,
		vm;
	var controller = {
		init: function() {
			if(!user.checkLogin()) return;
			api.setTitle(L('html_title_user'));
			api.footerShow(1); //显示底部导航
			api.initFooter('user');
			api.headerShow(0);

			this.render();

		},
		render: function() {
			var that = this;
			$('#common').html('<div id="user"></div>');
			vm = new Vue({
				el: '#user',
				data: {
					user: {
						name: user.getUsername() ? user.getUsername() : '--',
						headimgurl: user.getWeixinAvatar()
					},
					userFund: {},
					show: true
				},
				filters: {
					priceFormat: function(v) {
						return v ? util.priceFormat(2, v) : '';
					},
					/*
					 * 盈亏比例=总持仓货款/总市值
					 */
					plRate: function(user) {
						var v = (1 * 100) / 2;
						return v >= 0 ? '+ ' + v + '%' : '- ' + v + '%';
					},
					pl: function(v) {
						var prefix = '';
						if(!isNaN(v)) {
							if(v > 0) {
								prefix = '+';
							}
							return prefix + util.priceFormat(2, v);
						} else {
							return '-'
						}
					},
					rate: function(v) {
						var prefix = '';
						if(!isNaN(v)) {
							if(v > 0) {
								prefix = '+';
							}
							return prefix + v + '%';
						} else {
							return '-'
						}
					}
				},
				methods: {},
				template: tpl
			});

			that.initUserInfo();

		},
		initUserInfo: function() {
			api.userInfo(function(rs) {
				if(rs.retcode == '0') {
					vm.userFund = rs.data;
				} else {
					util.msg('用户未登录或超时！');
					location.hash = "#/login";
				}
			});
		},
		onRouteChange: function() {
			if(vm) vm.show = false;
		}
	}
	return controller;
})